package org.jeecg.modules.wlhy.complaint.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.wlhy.complaint.entity.HyComplaint;
import org.jeecg.modules.wlhy.complaint.service.IHyComplaintService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

 /**
 * @Description: 投诉表
 * @Author: jeecg-boot
 * @Date:   2020-12-16
 * @Version: V1.0
 */
@Api(tags="投诉表")
@RestController
@RequestMapping("/complaint/hyComplaint")
@Slf4j
public class HyComplaintController extends JeecgController<HyComplaint, IHyComplaintService> {
	 @Autowired
	 private IHyComplaintService hyComplaintService;
	 @Autowired
	 private IHyCustomerService hyCustomerService;


	/**
	 * 分页列表查询
	 *
	 * @param hyComplaint
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="投诉表-分页列表查询待审核", notes="投诉表-分页列表查询待审核")
	@GetMapping(value = "/list1")
	public Result<?> queryPageList1(HyComplaint hyComplaint,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		List<String> list = new ArrayList<>();
		list.add("0");
		return queryList(hyComplaint, pageNo, pageSize, req, list);
	}
	 @ApiOperation(value="投诉表-分页列表查询处理中", notes="投诉表-分页列表查询处理中")
	 @GetMapping(value = "/list2")
	 public Result<?> queryPageList2(HyComplaint hyComplaint,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 List<String> list = new ArrayList<>();
		 list.add("2");
		 list.add("3");
		 return queryList(hyComplaint, pageNo, pageSize, req, list);
	 }

	 @ApiOperation(value="投诉表-分页列表查询已完结", notes="投诉表-分页列表查询已完结")
	 @GetMapping(value = "/list3")
	 public Result<?> queryPageList3(HyComplaint hyComplaint,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 List<String> list = new ArrayList<>();
		 list.add("4");
		 return queryList(hyComplaint, pageNo, pageSize, req, list);
	 }

	 @ApiOperation(value="投诉表-分页列表查询未受理", notes="投诉表-分页列表查询未受理")
	 @GetMapping(value = "/list4")
	 public Result<?> queryPageList4(HyComplaint hyComplaint,
									 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									 HttpServletRequest req) {
		 List<String> list = new ArrayList<>();
		 list.add("1");
		 return queryList(hyComplaint, pageNo, pageSize, req, list);
	 }
	 //分页列表查询
	public Result<?> queryList(HyComplaint hyComplaint, Integer pageNo, Integer pageSize, HttpServletRequest req, List<String> complaintStatus) {
		QueryWrapper<HyComplaint> queryWrapper = QueryGenerator.initQueryWrapper(hyComplaint, req.getParameterMap());
//		//1 系统管理员 2 货主角色
//		SysUser sysUser = userUtil.getUser();
//		String uType = sysUser.getUType();
//		if(!("1".equals(uType))){
//			queryWrapper.eq("create_by", sysUser.getUsername())
//				 .or().eq("complainant",sysUser.getRealname())
//				 .or().eq("respondent", sysUser.getRealname());
//		}
		//投诉状态
		queryWrapper.in("complaint_status", complaintStatus);

		Page<HyComplaint> page = new Page<HyComplaint>(pageNo, pageSize);
		IPage<HyComplaint> pageList = hyComplaintService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 @ApiOperation(value="投诉表-发起的投诉", notes="投诉表-发起的投诉")
	 @GetMapping(value = "/listSend")
	 public Result<?> queryPageListSend(HyComplaint hyComplaint,
										@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
										HttpServletRequest req) {
		 //拿到货主数据
		 LoginUser loginUser = UserUtil.getLoginUser();
		 String uType = loginUser.getUType();
		 String userId = loginUser.getId();
		 QueryWrapper<HyCustomer> customerWrapper = new QueryWrapper<>();
		 customerWrapper.eq("user_id", userId);
		 HyCustomer customer = hyCustomerService.getOne(customerWrapper);
		 if(customer == null) return Result.error("货主数据不存在！");


		 QueryWrapper<HyComplaint> queryWrapper = QueryGenerator.initQueryWrapper(hyComplaint, req.getParameterMap());
		 queryWrapper.eq("complainant",customer.getId());
		 Page<HyComplaint> page = new Page<HyComplaint>(pageNo, pageSize);
		 IPage<HyComplaint> pageList = hyComplaintService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 @ApiOperation(value="投诉表-收到的投诉", notes="投诉表-收到的投诉")
	 @GetMapping(value = "/listReceive")
	 public Result<?> queryPageListReceive(HyComplaint hyComplaint,
										   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
										   HttpServletRequest req) {
		 //拿到货主数据
		 LoginUser loginUser = UserUtil.getLoginUser();
		 String uType = loginUser.getUType();
		 String userId = loginUser.getId();
		 QueryWrapper<HyCustomer> customerWrapper = new QueryWrapper<>();
		 customerWrapper.eq("user_id", userId);
		 HyCustomer customer = hyCustomerService.getOne(customerWrapper);
		 if(customer == null) return Result.error("货主数据不存在！");

		 QueryWrapper<HyComplaint> queryWrapper = QueryGenerator.initQueryWrapper(hyComplaint, req.getParameterMap());
		 queryWrapper.eq("respondent", customer.getId());
		 Page<HyComplaint> page = new Page<HyComplaint>(pageNo, pageSize);
		 IPage<HyComplaint> pageList = hyComplaintService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	/**
	 *   创建投诉
	 *
	 * @param hyComplaint
	 * @return
	 */
	@AutoLog(value = "投诉表-添加")
	@ApiOperation(value="投诉表-添加", notes="投诉表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyComplaint hyComplaint) {
		return hyComplaintService.create(hyComplaint);
	}

	/**
	 * 编辑
	 * @param hyComplaint
	 * @return
	 */
	@AutoLog(value = "投诉表-编辑")
	@ApiOperation(value="投诉表-编辑", notes="投诉表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyComplaint hyComplaint) {
		hyComplaintService.updateById(hyComplaint);
		return Result.OK("编辑成功!");
	}

	//审核
	@AutoLog(value = "投诉表-审核")
	@ApiOperation(value="投诉表-审核", notes="投诉表-审核")
	@PutMapping(value = "/check")
	public Result<?> check(@RequestBody HyComplaint hyComplaint) {
	 	return hyComplaintService.check(hyComplaint);
	}
	//举证
	@AutoLog(value = "投诉表-举证")
	@ApiOperation(value="投诉表-举证", notes="投诉表-举证")
	@PutMapping(value = "/proof")
	public Result<?> proof(@RequestBody HyComplaint hyComplaint) {
		return hyComplaintService.proof(hyComplaint);
	}
 	//举证结束
	@AutoLog(value = "投诉表-举证结束")
	@ApiOperation(value="投诉表-举证结束", notes="投诉表-举证结束")
	@PutMapping(value = "/proofEnd")
	public Result<?> proofEnd(@RequestBody HyComplaint hyComplaint) {
		return hyComplaintService.proofEnd(hyComplaint);
	}
	//处理
	@AutoLog(value = "投诉表-处理")
	@ApiOperation(value="投诉表-处理", notes="投诉表-处理")
	@PutMapping(value = "/handle")
	public Result<?> handle(@RequestBody HyComplaint hyComplaint) {
		return hyComplaintService.handle(hyComplaint);
	}
	//完结投诉
	@AutoLog(value = "投诉表-完结投诉")
	@ApiOperation(value="投诉表-完结投诉", notes="投诉表-完结投诉")
	@PutMapping(value = "/finish")
	public Result<?> finish(@RequestBody HyComplaint hyComplaint) {
		return hyComplaintService.finish(hyComplaint);
	}
	//满意度评价
	@AutoLog(value = "投诉表-满意度评价")
	@ApiOperation(value="投诉表-满意度评价", notes="投诉表-满意度评价")
	@PutMapping(value = "/evaluate")
	public Result<?> evaluate(@RequestBody HyComplaint hyComplaint) {
		return hyComplaintService.evaluate(hyComplaint);
	}

	/**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @ApiOperation(value="投诉表-通过id查询", notes="投诉表-通过id查询")
	 @GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		 HyComplaint hyComplaint = hyComplaintService.getById(id);
		 if(hyComplaint==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(hyComplaint);
	 }

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "投诉表-通过id删除")
	@ApiOperation(value="投诉表-通过id删除", notes="投诉表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyComplaintService.removeById(id);
		return Result.OK("删除成功!");
	}

    /**
    * 导出excel
    *
    * @param request
    * @param hyComplaint
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HyComplaint hyComplaint) {
        return super.exportXls(request, hyComplaint, HyComplaint.class, "投诉表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HyComplaint.class);
    }
}
